package top.shiyiri.shops.controller;

import com.alibaba.fastjson.JSONObject;
import top.shiyiri.shops.bean.ProTotalCount;
import top.shiyiri.shops.service.ProTotalCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author Aunean
 * @date 2021/12/28
 */
@Controller
public class ProTotalCountController {

    @Autowired
    private ProTotalCountService service;

    @ResponseBody
    @RequestMapping("/proTotalCountByPage")
    public String proTotalCountByPage(int page, HttpSession session) {

        List<ProTotalCount> list = service.proTotalCountByPage(page, 5);

        int lines = service.totalLine();

        int pages = lines % 5 == 0? lines / 5: lines / 5 + 1;

        session.setAttribute("pages", pages);
        session.setAttribute("page", page);

        return JSONObject.toJSONString(list);
    }

    @ResponseBody
    @RequestMapping("/getproTotalCountPages")
    public Integer getproTotalCountPages(HttpSession session) {

        return (Integer) session.getAttribute("pages");
    }


    @ResponseBody
    @RequestMapping("/proTotalCount")
    public String proTotalCount() {

        List<ProTotalCount> list = service.proTotalCount();

        return JSONObject.toJSONString(list);
    }
}
